/*
 * Copyright (c) 2017, Iotelligent Corporation
 * All rights reserved.
 *
 * Description:
 *
 *   This file defines the rf link configuration:
 *   - RF link parameters
 *   - Board-level definitions
 */

#include "ibat2000_conf.h"
#include "rev.h"

/* Firmware version Major, Minor, Maintenance */
const uint8_t fw_version[] = {0, 0, 10};
const uint8_t build_date[] = __DATE__;
const uint8_t build_time[] = __TIME__;

/* Boot mode indicator */
uint8_t boot_mode;

/* Default RF link parameters and board-level config */
param_cfg_t g_RfPara =
{
    /*************************** module info **********************************/
    /* Module serial number */
    DEFAULT_DEVICE_SERIALS_NUMBER,
    /* Module address */
    DEFAULT_DEVICE_COMM_ADDRESS,

    /*************************** calibration params ***************************/
    /* Corrected value of chip tx dc offset */
    0,
    /* Corrected value for Forward power measurement (dB) */
    0.0f,
    /* Corrected value for RxIN power measurement (dB) */
    0.0f,

    /*************************** rf output power ******************************/
    /* RF carrier status (0: Off, 1: On) */
    IOT_OFF,
    /* External PA status (0: Disable, 1: Enable) */
    IOT_DISABLE,
    /* External PA bias voltage (0 ~ 3000 mV) */
    3000,
    /* Tx output power (dBm) */
    20,
    /* Tx gain control */
    0x800,

    /*************************** rf antenna control ***************************/
    /* Antenna returnloss threshold (dB) */
    DEFAULT_RF_ANTENNA_RL_THR,
    /* Antenna auto switch mode (0: Disable, 1: Enable) */
    IOT_DISABLE,
    /* Antenna attached status */
    0,
    /* Current working antenna idx (0: ANT-1 ~ 3: ANT-4, 0xf0: Not avaliable) */
    0,//ANT_NOT_ATTACHED,
    /* Antenna switch interval */
    2000,

    /*************************** rf hopping control ***************************/
    /* RF regulatory region */
    IOT_REGION_CHINA_2,
    /* RF working frequency (KHz) */
    922375,
    /* RF hopping mode (0: Disable, 1: Enable) */
    IOT_DISABLE,
    /* RF hopping interval (10 ~ 65535 ms) */
    2000,

    /*************************** rf modulation control ************************/
    /* R=>T modulation type (0: DSB-ASK, 1: SSB-ASK, 2: PR-ASK) */
    MODU_MODE_DSB_ASK,
    /* R=>T modulation depth (30: 30% ~ 100: 100%) */
    100,

    /*************************** rf link parameters ***************************/
    /* RFID Protocol (0: EPC/18K-6C, 1: GB, 2: GJB) */
    STD_MODE_HB,//STD_MODE_EPC,

    /* EPC/18K-6C - R=>T link profile (7: 128k, 11: 320k, 14: 64k, 8: 160k */
    0x08,
    /* EPC/18K-6C - R=>T Tari value (0: 6.25us, 1: 12.5us, 2: 25us) - RFU */
    EPC_TARI_12P5,
    /* EPC/18K-6C - R=>T RTcal value (ns) - RFU */
    0,
    /* EPC/18K-6C - R=>T TRcal value (ns) - RFU */
    0,
    /* EPC/18K-6C - R=>T divide ratio (0: 8, 1: 64/3) */
    0,
    /* EPC/18K-6C - T=>R link frequency - RFU */
    0,

    /* GB/GJB R=>T Tc value (0: 6.25us, 1: 12.5us) */
    GB_GJB_TC_6P25,
    /* GB T=>R link frequency */
    GB_BLF_64K,
    /* GJB T=>R link frequency */
    GJB_BLF_160K,

    /* T=>R data encoding (0: FM0, 1: M2, 2: M4, 3: M8) */
    M_MILLER2,//M_FM0,
    /* T=>R extended preamble */
    USE_PILOT_TONE,

    /*************************** protocol parameters **************************/
    /* EPC/18K-6C - anti-collision algorithm (0: Fixed-Q, 1: Dynamic-Q) */
    IOT_ENABLE,
    /* EPC/18K-6C - Q value for Fixed-Q algorithm */
    4,
    /* EPC/18K-6C - Default Q value for Dynamic-Q algorithm */
    4,
    /* EPC/18K-6C - Min Q value for Dynamic-Q algorithm */
    0,
    /* EPC/18K-6C - Max Q value for Dynamic-Q algorithm */
    15,

    /*************************** firmware features ****************************/
    /* The idle timeout for system entring low power mode (ms) */
    TIMER_SW_SLEEP_VALUE
};

/* default hopping table of each region */
static const rf_hoptable_t rf_hoptable_china_2 =
{
    IOT_REGION_CHINA_2,
    20,
    0,
    {
        920125, 921125, 922125, 923125, 924125,
        920375, 921375, 922375, 923375, 924375,
        920625, 921625, 922625, 923625, 924625,
        920875, 921875, 922875, 923875, 924875
    }
};

static const rf_hoptable_t rf_hoptable_fcc =
{
    IOT_REGION_FCC,
    50,
    0,
    {
        902750, 903750, 904750, 905750, 906750,
        907750, 908750, 909750, 910750, 911750,
        912750, 913750, 914750, 915750, 916750,
        917750, 918750, 919750, 920750, 921750,
        922750, 923750, 924750, 925750, 926750,
        903250, 904250, 905250, 906250, 907250,
        908250, 909250, 910250, 911250, 912250,
        913250, 914250, 915250, 916250, 917250,
        918250, 919250, 920250, 921250, 922250,
        923250, 924250, 925250, 926250, 927250
    }
};

static const rf_hoptable_t rf_hoptable_japan =
{
    IOT_REGION_JAPAN,
    8,
    0,
    {
        952200, 952800, 953400, 952400, 953000,
        953600, 952600, 953200
    }
};

static const rf_hoptable_t rf_hoptable_malaysia =
{
    IOT_REGION_MALAYSIA,
    7,
    0,
    {
        919500, 920500, 921500, 922500, 920000,
        921000, 922000
    }
};

static const rf_hoptable_t rf_hoptable_etsi =
{
    IOT_REGION_ETSI,
    4,
    0,
    {
        865700, 866300, 866900, 867500
    }
};

static const rf_hoptable_t rf_hoptable_china_1 =
{
    IOT_REGION_CHINA_1,
    20,
    0,
    {
        840125, 841125, 842125, 843125, 844125,
        840375, 841375, 842375, 843375, 844375,
        840625, 841625, 842625, 843625, 844625,
        840875, 841875, 842875, 843875, 844875
    }
};

const rf_hoptable_t *rf_hoptable_default[IOT_REGION_MAX_NUM] =
{
    &rf_hoptable_china_2,
    &rf_hoptable_fcc,
    &rf_hoptable_japan,
    &rf_hoptable_malaysia,
    &rf_hoptable_etsi,
    &rf_hoptable_china_1
};

#if (RF_MAX_TX_PORTDEF == 4)
/*
 * rf switch cfg for each antenna
 *
 * +-----+-----+-----+
 * |  V1 |  V2 | ANT |
 * +-----+-----+-----+
 * |  0  |  0  |  1  |
 * +-----+-----+-----+
 * |  0  |  1  |  2  |
 * +-----+-----+-----+
 * |  1  |  1  |  3  |
 * +-----+-----+-----+
 * |  1  |  0  |  4  |
 * +-----+-----+-----+
 */
const rf_switch_cfg_t rf_ant_cfg[RF_MAX_TX_PORTDEF] =
{
    {GPIO_DATA_LOW, GPIO_DATA_LOW },    // ANT-1
    {GPIO_DATA_LOW, GPIO_DATA_HIGH},    // ANT-2
    {GPIO_DATA_HIGH, GPIO_DATA_HIGH},   // ANT-3
    {GPIO_DATA_HIGH, GPIO_DATA_LOW }    // ANT-4
};
#else
/*
 * For single antenna work around:
 * Antenna will not actually be switched for single antenna solution.
 */
const rf_switch_cfg_t rf_ant_cfg[RF_MAX_TX_PORTDEF] =
{
    {GPIO_DATA_LOW, GPIO_DATA_LOW }
};
#endif

__weak int board_rf_init( void )
{
    return IOT_SUCCESS;
}

int rf_para_init( void )
{
    int status = IOT_SUCCESS;
    memset( &rf_hoptable, 0, sizeof( rf_hoptable_t ) );
    /* apply the default hopping table */
    if( g_RfPara.rf_region < IOT_REGION_MAX_NUM )
    {
        memcpy( &rf_hoptable, rf_hoptable_default[g_RfPara.rf_region],
                sizeof( rf_hoptable_t ) );
    }
    /* call board specified initilization about RF/RF para */
    status = board_rf_init();
    return status;
}

/*
 * Show the basic info:
 * - Firmware version and build time
 * - Chip model
 * - Boot mode
 * - Board name
 * - System clk
 */
void board_info( void )
{
    TRACE_INFO( "\n%s boot mode...",
                ( boot_mode == SYSCON_BO_ROM_BOOT ) ? "ROM" : "SPI" );
    TRACE_INFO( "\nFirmware v%d.%d.%d - build %d (%s - %s)",
                fw_version[0], fw_version[1], fw_version[2],
                build_revision, build_date, build_time );
    TRACE_INFO( "\nCopyright (c) 2017, Iotelligent Inc." );
    TRACE_INFO( "\n" );
    TRACE_INFO( "\nBoard: %s", BOARD_NAME );
    TRACE_INFO( "\nChip : iBAT%d", sys_get_chip_model() );
    TRACE_INFO( "\nClock: %d MHz", system_clk / 1000000 );
    TRACE_INFO( "\nIRAM : %s", CHIP_IRAM );
    TRACE_INFO( "\nDRAM : %s", CHIP_DRAM );
    TRACE_INFO( "\n\n" );
}
